<?php
namespace Client\Controller;
use Think\Controller;
class ApiController extends Controller {
	
    public static $session;		//SESSION
    
	public function _initialize(){
		self::$session=$_SESSION;
		
		
	}
	//===================
    public function index(){
		$this->display();
    }
	
	
	//预约接口
	public function add_reserve(){
		if(IS_POST){
			$p=I('post.');
			$or=M('bb_order')->where(array('openid'=>self::$session['openid']))->find();
			if(!$or){
				$userset=M('bb_user_set')->find();
				$U_integral['integral']=(self::$session['user']['integral']+$userset['conp_score']);
				$U_integral['id']=self::$session['user']['id'];
				M('client_user')->save($U_integral);
				$log['openid']=self::$session['openid'];
				$log['type']='1';   //积分日志
				$log['addtime']=time();   
				$log['msg']='积分增加：首次下单获得'.$userset['conp_score'].'积分';
				M('log_msg')->add($log);
			}
			$p['orderid']=date("ymdHis",time()).'000'.rand(100000,999999);
			$p['userid']=self::$session['user']['id'];
			$p['openid']=self::$session['openid'];
			$p['gps_address']=self::$session['wz']['address'];
			$p['gps_lon']=self::$session['wz']['longitude'];
			$p['gps_lat']=self::$session['wz']['latitude'];
			$p['prckup_dtm']=$p['pickup_date']." ".$p['pickup_time'];
			$p['addtime']=date("Y-m-d H:i:s",time());
			$p['addstamp']=time();
			
			if($p['daityoe']==1){
				$p['total_price']=88;
			}else if($p['daityoe']==2){
				$p['total_price']=188;
			}
			$re=M('bb_order')->add($p);
			if($re){
				$info['status']=1;
				$info['msg']='ok！';
				
				//========================
				$u_phone=M('client_user')->where(array('openid'=>self::$session['openid']))->find();
				if($u_phone['phone']=='' || $u_phone['name']==''){
					$u_phone['phone']=$p['phone'];
					$u_phone['name']=$p['name'];
					M('client_user')->save($u_phone);
				}
				//========================
				//推送给阿姨端消息
				$tp=new \bb\template();
				$array=array(
					'name'=>$p['name'],
					'phone'=>$p['phone'],
					'time'=>$p['prckup_dtm'],
					'orderid'=>$p['orderid'],
					'neirong'=>'快去抢单把'
				);
				
				$options['appid']=C('BB_APPID_auntie');
				$options['secret']=C('BB_SECRET_auntie');
				$wx=new \bb\Wechat($options);
	//========================
				$lat=$p['gps_lat'];
				$lon=$p['gps_lon'];
				$squares = returnSquarePoint($lon, $lat, C('BB_NEAR_QM'));  //最后一个参数是查找附进多少千米的订单
				$info_sql = "select * from `bb_auntie` where status='1' and gps_lat<>0 and gps_lat>{$squares['right-bottom']['lat']} and gps_lat<{$squares['left-top']['lat']} and gps_lon>{$squares['left-top']['lng']} and gps_lon<{$squares['right-bottom']['lng']} ORDER BY `addtime` "; 
				$auntie=M('bb_auntie')->query($info_sql);

				foreach ($auntie as $k => $v) {
					$templatedata=$tp->enddata('put_order',$v['openid'],$array);	//组合模板数据
					$wx->sendTemplate($templatedata);	//发送模板
				}
				//==========

			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);
		}
	}
	//获取订单价格
	public function get_order(){
		if(IS_POST){
			$p=I('post.');
			$order=M('bb_order')->where(array('orderid'=>$p['orderid']))->find();
			if($order){
				$info['status']=1;
				$info['msg']=$order;
			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);
		}
	}
	
	//修改用户信息
	public function edit_user(){
		if(IS_POST){
			$p=I('post.');
			$p['edittime']=time();
			$re=M('client_user')->save($p);
			if($re){
				$info['status']=1;
				$info['msg']='ok！';
				$user=M('client_user')->where(array('id'=>$p['id']))->find();
				$_SESSION['user']=$user;
			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);
		}
	}
	
	//添加多个地址
	public function add_address(){
		if(IS_POST){
			$p=I('post.');
			if($p['id']){
				if($p['is_def']==1){
					M('bb_address')->where(array('openid'=>self::$session['openid']))->save(array('is_def'=>0));
					$u['name']=$p['name'];
					$u['phone']=$p['mobile'];
					$u['address']=$p['addr'];
					M('client_user')->where(array('openid'=>self::$session['openid']))->save($u);
					$user=M('client_user')->where(array('openid'=>self::$session['openid']))->find();
					$_SESSION['user']=$user;
				}
				$re=M('bb_address')->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='ok！';
				}else{
					$info['status']=0;
					$info['msg']='no！';
				}
				
			}else{
				if($p['is_def']==1){
					M('bb_address')->where(array('openid'=>self::$session['openid']))->save(array('is_def'=>0));
					$u['name']=$p['name'];
					$u['phone']=$p['mobile'];
					$u['address']=$p['addr'];
					M('client_user')->where(array('openid'=>self::$session['openid']))->save($u);
					$user=M('client_user')->where(array('openid'=>self::$session['openid']))->find();
					$_SESSION['user']=$user;
				}
				$p['createtm']=date("Y-m-d H:i:s",time());
				$p['createip']=get_client_ip();
				$p['openid']=self::$session['openid'];
				$re=M('bb_address')->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='ok！';
				}else{
					$info['status']=0;
					$info['msg']='no！';
				}
			}
			$this->ajaxreturn($info);
		}
	}
	//手动获取用户地理位置
	public function edit_area(){
		if(IS_POST){
			$p=I('post.');
			$longitude=$p['longitude'];
			$latitude=$p['latitude'];
			if($latitude && $longitude){
				//微信坐标转换成百度坐标
				$url = "http://api.map.baidu.com/geoconv/v1/?coords=".$longitude.",".$latitude."&from=1&to=5&ak=".C('BB_AK');
		 		$re = get_curl($url);
				$zb = json_decode($re,true);
				if($zb['status']==0){
					$longitude=$zb['result']['0']['x'];
					$latitude=$zb['result']['0']['y'];
					//根据百度坐标换取位置
					$url = 'http://api.map.baidu.com/geocoder?location='.$latitude.','.$longitude.'&coord_type=bd09ll&output=json&src=jt';
					$addressData = get_curl($url);
					$address = json_decode($addressData,true);
					if($address['status']=='OK'){
						$addres['address']=$address['result']['formatted_address'];
						$addres['longitude']=$address['result']['location']['lng'];
						$addres['latitude']=$address['result']['location']['lat'];
						$addres['province']=$address['result']['addressComponent']['province'];		//省
						$addres['city']=$address['result']['addressComponent']['city'];						//市
						$addres['district']=$address['result']['addressComponent']['district'];			//区
						$dq=$addres['city']."  ".$addres['district'];
						if($dq){
							$info['status']=1;
							$info['msg']=$dq;
						}else{
							$info['status']=0;
							$info['msg']='no！';
						}
						$this->ajaxreturn($info);
					}
				}
			}
		}
	}
	


//获取用户所在的城市
	public function get_city(){
		if(IS_POST){
			$p=I('post.');
			$longitude=$p['longitude'];
			$latitude=$p['latitude'];
			if($latitude && $longitude){
				//微信坐标转换成百度坐标
				$url = "http://api.map.baidu.com/geoconv/v1/?coords=".$longitude.",".$latitude."&from=1&to=5&ak=".C('BB_AK');
		 		$re = get_curl($url);
				$zb = json_decode($re,true);
				if($zb['status']==0){
					$longitude=$zb['result']['0']['x'];
					$latitude=$zb['result']['0']['y'];
					//根据百度坐标换取位置
					$url = 'http://api.map.baidu.com/geocoder?location='.$latitude.','.$longitude.'&coord_type=bd09ll&output=json&src=jt';
					$addressData = get_curl($url);
					$address = json_decode($addressData,true);
					if($address['status']=='OK'){
						$addres['address']=$address['result']['formatted_address'];
						$addres['longitude']=$address['result']['location']['lng'];
						$addres['latitude']=$address['result']['location']['lat'];
						$addres['province']=$address['result']['addressComponent']['province'];		//省
						$addres['city']=$address['result']['addressComponent']['city'];						//市
						$addres['district']=$address['result']['addressComponent']['district'];			//区
						$dq=$addres['city'];
						if($dq){
							
							$udata=M('weixin_client_user')->where(array('openid'=>self::$session['openid']))->find();
							$udata['latitude']=$addres['latitude'];
							$udata['longitude']=$addres['longitude'];
							M('weixin_client_user')->save($udata);	
							
							$info['status']=1;
							$info['msg']=$dq;
						}else{
							$info['status']=0;
							$info['msg']='no！';
						}
						$this->ajaxreturn($info);
					}
				}
			}
		}
	}

	//取消订单
	public function order_set(){
		$orderid=I('orderid');
		$m=M('bb_order');
		if($orderid){
			$set=I('set');
			if($set==9){
				$re=$m->where(array('orderid'=>$orderid))->save(array('status'=>'9'));
				if($re){
					$info['status']=1;
					$info['msg']='yes！';
				}else{
					$info['status']=0;
					$info['msg']='no！';
				}
			}

			$this->ajaxreturn($info);
		}
	}
	
	
	//确认订单
	public function order_qrset(){
		$orderid=I('orderid');
		$m=M('bb_order');
		if($orderid){
			$re=$m->where(array('orderid'=>$orderid))->save(array('status'=>'6'));
			if($re){
				$info['status']=1;
				$info['msg']='yes！';
			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);
		}
	}
	//发送验证码
	public function code(){
		if(IS_POST){
			$code=$_SESSION['yzm']=rand(100000,999999);
			$phone=I('phone');
			$sms=new \bb\Sms();
			$conent="您好，本次短信验证码为".$code;
			$re=$sms->endsms($phone,$conent);
			if($re){
				$info['status']=1;
				$info['msg']='yes！';
			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
		}else{
			$info['status']=0;
			$info['msg']='no！';
		}
		$this->ajaxreturn($info);
	}
	
	
	//修改手机
	public function edit_phone(){
		if(IS_POST){
			$p=I('post.');
			if($p['yzm']==$_SESSION['yzm']){
				$save['edittime']=time();
				$save['phone']=$p['phone'];
				$re=M('client_user')->where(array('openid'=>self::$session['openid']))->save($save);
				if($re){
					$info['status']=1;
					$info['msg']='ok！';
					$user=M('client_user')->where(array('openid'=>self::$session['openid']))->find();
					$_SESSION['user']=$user;
				}else{
					$info['status']=0;
					$info['msg']='no！';
				}
			}else{
				$info['status']=2;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);		
		}
	}
}